package typejump.units;

import java.lang.reflect.Field;

public class Length {	
	/** Meters (base unit) */
	public static final Unit M = new Unit() {
		public Unit getBase() {
			return this;
		}
		public double toBase(double unit) {
			return unit;
		}
		public double fromBase(double base) {
			return base;
		}
		public String getName() {
			return "meter";
		}
		public String getSuffix() {
			return "m";
		}		
	};
	
	/** Pixels */
	public static final Unit PX = Unit.createUnit(M, "pixel", "px", "px/m");
	
	/** Centimeters */
	public static final Unit CM = Unit.createUnit(M, "centimeter", "cm", 100);
	
	/** Kilometers */
	public static final Unit KM = Unit.createUnit(M, "kilometer", "km", 0.001);
	
	public static final Unit FT = Unit.createUnit(M, "feet", "ft", 3.2808399);
	
	public static final Unit MI = Unit.createUnit(M, "miles", "mi", 0.000621371192);
	
	public static Unit findUnit(String suffix) {
		Field[] fields = Length.class.getFields();
		for (Field f : fields) {
			if (f.getType() == Unit.class) {
				try {
					Unit u = (Unit)f.get(null);
					if (u.getSuffix().equalsIgnoreCase(suffix)) {
						return u;
					}
				} catch (Exception e) {
					System.err.println(e);
				}
			}
		}
		
		return null;
	}
	

	public static Unit getDefault() {
		return Length.PX;
	}
}
